﻿using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace YLServerInterface.UserControls.DiagramDesigner
{
    public class MoveThumb : Thumb
    {
        public MoveThumb()
        {
            DragDelta += new DragDeltaEventHandler(this.MoveThumb_DragDelta);
        }
        double limited(double x)
        {
            return x < 0 ? 0 : x;
        }

        private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Control designerItem = this.DataContext as Control;

            if (designerItem != null)
            {
                Canvas parent = designerItem.Parent as Canvas;
                double left = Canvas.GetLeft(designerItem);
                double top = Canvas.GetTop(designerItem);
                double maxLeft =  parent.ActualWidth - designerItem.Width;
                double maxTop =  parent.ActualHeight- designerItem.Height;


                double nLeft = left + e.HorizontalChange;
                double nTop = top + e.VerticalChange;
                nLeft = nLeft < 0 ? 0 : nLeft;
                nLeft = nLeft > maxLeft ? maxLeft : nLeft;
                nTop = nTop < 0 ? 0 : nTop;
                nTop = nTop > maxTop ? maxTop : nTop;
                Canvas.SetLeft(designerItem, nLeft);
                Canvas.SetTop(designerItem, nTop);
            }
        }
    }
}
